!!ARBvp1.0
# 
# This shader provides sprite-based textured lights.  The weird input params are due to us not using
# 4-part tex coords in hl_xdl...if we had 4-part tex coords in hl_xdl this would be moot!
#
# Inputs:
#
#	Vertex 		- the coordintaes of the light.
#	Normal 		- actually contains RGB color tint information!
#	Color 		- cotains 4-part "params"
#	Tex unit 0 	- lower left of texture to use
#	Tex unit 1 	- size, alpha of light
#
# Outputs:
#
#	Position	- transformed clip space light pos
#	point size	- calculated lite size
#	color		- R and G contain tex coord offset S & T.  A contains alpha of light

ATTRIB	iPos		= vertex.position;
ATTRIB	iCol		= vertex.color;
ATTRIB	iNrm		= vertex.normal;
ATTRIB	iTex0		= vertex.texcoord[0];
ATTRIB	iTex1		= vertex.texcoord[1];

PARAM 	mvp[4]      = { state.matrix.mvp };
PARAM 	mv[4]       = { state.matrix.modelview };

PARAM	light_time	= program.local[0];
PARAM	light_info	= program.local[1];
PARAM	one_two		= { 1, 2, -1, 0 };
PARAM	zero_one	= { 0, 1 };

OUTPUT oPos         = result.position;
OUTPUT oSize 		= result.pointsize;
OUTPUT oCol			= result.color;

TEMP	eye_pos;
TEMP	light_size;
TEMP	total_brightness;
TEMP	inverse;
TEMP	is_night;
#######################################################################################################################

# Transform the light location
DP4   	oPos.x, mvp[0], iPos;	
DP4   	oPos.y, mvp[1], iPos;
DP4   	oPos.z, mvp[2], iPos;
DP4   	oPos.w, mvp[3], iPos;

# Color: XY comes from tex 0 (tex coord offset).  W (alpha) comes from "intensity" of light.

MOV oCol.xy, iTex0;

# Convert the light location to eye-space. 

DP4	eye_pos.z, mv[2], iPos;

# Fogging based on eye-space
MAD		total_brightness.w, eye_pos.z, light_time.z, light_time.w;
MIN		total_brightness.w, zero_one.y, total_brightness.w;
MAX		total_brightness.w, zero_one.x, total_brightness.w;


# Brightness calculation for flashing lights:

#	iCol.x = phase
#	iCol.y = frequency
#	iCol.z = amplitude
#	iCol.w = flashing daytime level
#
# 	inverse.z = base levels
# 	invese.w = night brightness

SUB inverse, one_two.x, iCol;

ADD is_night.x, iPos.x, iPos.y;
ADD is_night.x, is_night.x, iPos.z;
FRC is_night.x, is_night.x;
SGE	is_night,light_time.x,is_night.x;
MUL is_night.x, is_night.x, inverse.w;

#	total_brightness.x = time * freq + phase
MAD	total_brightness.x, light_time.y, iCol.y, iCol.x;
FRC total_brightness.x, total_brightness.x;
MAD total_brightness.x, total_brightness.x, one_two.y, one_two.z;
ABS total_brightness.x, total_brightness.x;
MAD total_brightness.x, total_brightness.x, iCol.z, inverse.z;
MAD total_brightness.x, total_brightness.x, is_night.x, iCol.w;

# Size of light comes from tex coord 1 S, ends up in the oSize var.
# We calc the eye-space Z and recip-square it, for now at least.

MUL		light_size.x, light_info.y, iTex1.x;
RCP 	eye_pos.w, -eye_pos.z;
POW		eye_pos.w, eye_pos.w, light_info.x;
MUL		total_brightness.z, eye_pos.w, light_size.x;
MAX 	oSize.x, light_info.z, total_brightness.z;
MUL		total_brightness.z, total_brightness.z, light_info.w;

# Final alpha assembly based on brightness and size

MIN total_brightness.y, one_two.x, total_brightness.z;
MUL total_brightness.x, total_brightness.y, total_brightness.x;
MUL total_brightness.x, total_brightness.y, total_brightness.x;
MUL total_brightness.x, total_brightness.w, total_brightness.x;

# Finally mult in with the light's intensity.

MUL	oCol.w,  total_brightness.x, iTex1.y;

END
